<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_business_supplier_form' })">
              {{ i18n('common.create') }}
            </el-button>

            <el-button
              class="el-button--cyan"
              icon="Filter"
              @click="router.push({ name: 'module_business_supplier_category_list' })">
              {{ i18n('supplier.category.info') }}
            </el-button>
          </div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="1 == dataForm.creator_id"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="general-situation">
        <div>
          <el-tag class="float-right p10 mr10" type="info" size="small" effect="dark" round>
            {{ totalPage }}
          </el-tag>
          <h5 class="float-right mr10">全部供应商</h5>
        </div>
      </div>



      <div class="admin_main_block_select color" style="padding: 0 20px;">
        <el-form label-position="right" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('supplier.title')">
                <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('supplier.title')"  clearable @change="getDataList(model)"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('supplier.organization_id')">
                <el-radio-group v-model="dataForm.creator_id" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in organizationList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('supplier.category_id')">
                <el-select
                  v-model="dataForm.category_id"
                  :placeholder="i18n('common.please_select') + i18n('supplier.category_id')" clearable @change="getDataList(model)">
                  <el-option v-for="(v, k) in categoryList" :label="v.title" :key="k" :value="v.id">
                    <span class="float-left">{{ v.title }}</span>
                    <span class="float-right">{{ v.total }}</span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('supplier.cooperation_level')">
                <el-radio-group v-model="dataForm.cooperation_level" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in cooperationLevelList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item :label="i18n('supplier.region_id')">
                <el-cascader v-model="dataForm.city_id" :placeholder="$t('common.please_select')+$t('supplier.region_id')" :options="addressList" :props="levelProps" clearable @change="getDataList(model)">
                  <template v-slot="{ node, data }">
                    <span>{{ data.title }}</span>
                    <span class="float-right">{{ data.total }}</span>
                  </template>
                </el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item :label="i18n('product.is_hidden')">
                <el-radio-group v-model="dataForm.is_hidden" @change="getDataList(model)">
                  <el-radio label="">{{ i18n('common.all') }}</el-radio>
                  <el-radio v-for="(v, k) in statusList" :key="k" :label="v.value">
                    {{ v.text }}
                    <span class="red">({{ v.total }})</span>
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('supplier.info')" min-width="240">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.category.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  <b>{{ scope.row.title }}</b>
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.category.title }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('supplier.cooperation_level')" width="100">
            <template v-slot="scope">
              {{ scope.row.cooperation_level.value }} 级
            </template>
          </el-table-column>

          <el-table-column :label="i18n('supplier.address')" width="200">
            <template v-slot="scope">
              {{ scope.row.addressInfo }}
            </template>
          </el-table-column>

          <el-table-column prop="sort" :label="i18n('common.sort')" width="80">
          </el-table-column>

          <el-table-column :label="$t('product.is_hidden')" width="80">
            <template v-slot="scope">
              <el-switch
                v-if="user.organization_id == scope.row.organization_id"
                v-model="scope.row.is_hidden.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.no')"
                :inactive-text="$t('common.yes')"
                style="--el-switch-on-color: #ff4949; --el-switch-off-color: #13ce66"
                @change="handleStatus($event, scope.row.id, 'is_hidden')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template v-slot="scope">
              <el-button
                v-if="user.organization_id == scope.row.organization_id"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_supplier_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="user.organization_id == scope.row.organization_id"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more" v-if="user.organization_id == scope.row.organization_id">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="dialogVisible = true;loadQrcodeUrl(scope.row.id)">
                      {{ i18n('supplier.preview') }}
                    </el-dropdown-item>
                    <el-dropdown-item @click="closeSupplierData(scope.row.id)">
                      {{ i18n('supplier.close') }}
                    </el-dropdown-item>
                    <el-dropdown-item @click="router.push({ name: 'module_business_supplier_contact_list', query: { supplier_id: scope.row.id } })">
                      {{ i18n('supplier.contact.info') }}
                    </el-dropdown-item>
                    <el-dropdown-item @click="router.push({ name: 'module_business_supplier_product_list', query: { supplier_id: scope.row.id } })">
                      {{ i18n('supplier.product.info') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="在小程序上查看" width="300" center>
      <div class="text-center mt20">
        <el-image class="qrcode" :src="qrcode_url"/>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="supplier-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"
  import { useStore } from "@/hooks/store/useStore"

  const { router, query, i18n, isAuth } = useBase()
  const { user } = useStore()

  const model = ref('business/supplier')

  const levelProps = ref({
    label: 'title',
    value: 'id',
    children: 'child',
    emitPath: false,
    checkStrictly: false
  })

  const dialogVisible = ref(false)
  const qrcode_url = ref('')
  const organizationList = ref([])
  const categoryList = ref([])
  const addressList = ref([])
  const cooperationLevelList = ref([])
  const statusList = ref([])

  const dataForm = reactive([
    'creator_id',
    'category_id',
    'city_id',
    'title',
    'cooperation_level',
    'is_recommend',
    'is_hidden',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadSearchList = () => {
    $http({
      url: $http.adornUrl('/business/supplier/search/data'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        organizationList.value = data.data.organization
        categoryList.value = data.data.category
        cooperationLevelList.value = data.data.cooperation_level
        addressList.value = data.data.address
        statusList.value = data.data.status
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadQrcodeUrl = (id) => {
    $http({
      url: $http.adornUrl('/business/qrcode/mini/data'),
      method: 'get',
      params: $http.adornParams({
        path: '/pages/resource/supplier/detail?id='+id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        qrcode_url.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const closeSupplierData = (id) => {
    $http({
      url: $http.adornUrl('/business/supplier/close'),
      method: 'post',
      params: $http.adornParams({
        id: id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        router.go(0)
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    dataForm.creator_id = 1

    getDataList(model)

    loadSearchList()
  })
</script>

<style scoped lang="scss">
  .qrcode {
    width: 10vw;
    height: 10vw;
  }
</style>
